<?php
class ml_model_album_dbAlbumfeed extends Lib_datamodel_db
{
    private $_status_array = array('ok'=>1,'del'=>9);
    private $_fields = array(
            'albumfeed_rid',
            'uid', 
            'content',
            'class_id',
            'album_id',
            'pic_id',
            'content_rid',
            'gd_info', 
            'reply_cnt', 
            'reply_user', 
            'from',
            'status',
            'ctime',
            'cip'
        );
    
    public function __construct() {
        
        $db_config = ml_factory::load_standard_conf ( 'dbAlbum' );
        parent::__construct ( 'albumfeed', $db_config ['albumfeed'] );
        $this->_fields = '`'.implode('`,`', $this->_fields).'`';
    }
    
    public function set_fields($fields){
        if(empty($fields))
            return false;
        $this->_fields = is_array($fields) ? implode(', ', $fields) : $fields; 
        return true;
    }
    protected function hook_after_fetch()
    {
        $data = $this->get_data();

        if(isset($data['gd_info']))
        {
            $data['gd_info'] = unserialize($data['gd_info']);
            $this->set_data($data);
        }else
        {
            foreach($data as $key=>$val)
            {
                if(isset($val['gd_info']))
                {
                    $val['gd_info'] =unserialize($val['gd_info']);
                }
                $arr[$key]=$val;
            }
            $this->set_data($arr);
        }

        return true;
    }
    protected function hook_before_write($array)
    {
        if(isset($array['gd_info']))
        {
            $array['gd_info'] = serialize($array['gd_info']);
        }
        return $array;
    }



    /**
     * 通过album_rid获取评论内容
     **/
    public function get_by_albumfeed_rid($rid)
    {
        $album_id = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($album_id , self::DB_SLAVE))
            return false;
        
        $sql = "select ".$this->_fields." from `".$this->table."` where `albumfeed_rid`='$rid'";
        $rs=$this->fetch_row($sql);
        return $rs;
    }
    
    /**
     * 通过album_id获取画报的评论列表
     */
    public function get_list_by_album_id($album_id, $begin, $count, $order='ctime DESC'){
        if(!$this->init_db($album_id , self::DB_SLAVE))
            return false;
        if($begin < 0)
            $begin = 0;
        $sql = "select ".$this->_fields." from `".$this->table."` where `album_id`='$album_id'  AND  `status`=".$this->_status_array['ok']." ORDER BY $order  limit $begin,$count";
        $rs=$this->fetch($sql);
        return $rs;
    }
    /**
     * 通过album_id获取画报的评论总数
     */
    public function get_count_by_album_id($album_id){
        if(!$this->init_db($album_id , self::DB_SLAVE  ))
            return false;
        
        $where = "`album_id`='$album_id'  AND  `status`=".$this->_status_array['ok'];
        $rs = $this->fetch_count($where);
        return  $rs;
            
    }
    
    /**
     * 插入评论内容
     */
    public function insert_content($data){
        $album_id = $data['album_id'];
        if(!$this->init_db($album_id , self::DB_MASTER ))
            return false;
        
        $array['albumfeed_rid'] = $data['albumfeed_rid'];
        $array['uid'] = $data['uid'];
        $array['content'] = $data['content'];
        $array['class_id'] = $data['class_id'];
        $array['album_id'] = $data['album_id'];
        $array['pic_id'] = $data['pic_id'];
        $array['content_rid'] = $data['content_rid'];
        if(!empty($data['gd_info']))
            $array['gd_info'] = $data['gd_info'];
        $array['from'] = empty($data['from']) ? ML_FROM_WEB : $data['from'];
        $array['status'] = $this->_status_array['ok'];
        $array['ctime'] = empty($data['ctime']) ? date('Y-m-d H:i:s') : $data['ctime'];
        $array['cip'] = empty($data['cip']) ? Tool_ip::get_real_ip() : $data['cip'];
        return $this->insert($array);
    }
    /**
     * 通过albumfeed_rid删除评论内容
     */
    public function del_by_albumfeed_rid($rid){
        $album_id = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($album_id , self::DB_MASTER))
            return false;
            
        $data['status'] = $this->_status_array['del'];
        $where = "`albumfeed_rid` = '$rid'";
        return $this->update( $data, $where, 1);
    }
    
    public function descr_reply_cnt_by_albumfeed_rid($rid){
        $album_id = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($album_id , self::DB_MASTER))
            return false;
        $sql = " update `".$this->table."` set `reply_cnt` = `reply_cnt`-1 where `albumfeed_rid`='$rid'";
        return $this->query($sql);
    }
    
    public function incr_reply_cnt_by_albumfeed_rid($rid){
        $album_id = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($album_id , self::DB_MASTER))
            return false;
        $sql = " update `".$this->table."` set `reply_cnt` = `reply_cnt`+1 where `albumfeed_rid`='$rid'";
        return $this->query($sql);
    }
}
?>